package mail

import (
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/util/gconv"
	ses "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ses/v20201002"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
	"strings"
)

type tencent struct {
}

var Tencent = new(tencent)

// 腾讯云发送短信
func (m *tencent) SendEmail(form, mailTo, replyTo, subject, content string, templateId int64) (interface{}, error) {
	secretId := g.Cfg().GetString( "mail.tencent.secret_id", "AKIDhJk0737VXcp5GL4C1jLrIYfkox79lgAS")
	secretKey := g.Cfg().GetString( "mail.tencent.secret_key", "GLQK3bx791hHfvzraklalkX8e4NbiicP")
	endpoint := g.Cfg().GetString( "mail.tencent.endpoint", "ses.tencentcloudapi.com")
	region := g.Cfg().GetString( "mail.tencent.region", "ap-hongkong")

	credent := common.NewCredential(secretId, secretKey)
	httpProfile := profile.NewHttpProfile()
	httpProfile.Endpoint = endpoint
	clientProfile := profile.NewClientProfile()
	clientProfile.HttpProfile = httpProfile

	client, err := ses.NewClient(credent, region, clientProfile)
	if err != nil {
		panic(err)
	}

	parmas := g.Map{
		"Destination": strings.Split(mailTo, ","),
		"Template": g.Map{
			"TemplateID": templateId,
			"TemplateData": content,
		},
		"FromEmailAddress": form,
		"Subject": subject,
		"ReplyToAddresses": replyTo,
	}
	request := ses.NewSendEmailRequest()
	request.FromJsonString(gconv.String(parmas))
	return client.SendEmail(request)
}